﻿using JewererBasic.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace JewererBasic.BO
{
    public class ProcessReportBO
    {
        public static List<ProcessReportModel> GetListOfProcesses(String orderId = null, int finishWeek = 0, int kindId = 0, String productId = null)
        {
            MSQDBEntities db = new MSQDBEntities();

            // result of this function
            List<ProcessReportModel> result = new List<ProcessReportModel>();

            // temp of list bag processes
            List<PROCESS> processes = db.PROCESSes.ToList();
            foreach (var p in processes)
            {
                result.Add(new ProcessReportModel() { ProcessId = p.Id, ProcessName = p.Name });
            }

            IEnumerable<ORDER> orders;

            if (String.IsNullOrEmpty(orderId) == false && finishWeek > 0)
            {
                orders = from d in db.ORDERs
                         where d.IdOrder.Contains(orderId) && d.FinishWeek == finishWeek
                         select d;
            }
            else if (String.IsNullOrEmpty(orderId) == false)
            {
                orders = from d in db.ORDERs
                         where d.IdOrder.Contains(orderId)
                         select d;
            }
            else if (finishWeek > 0)
            {
                orders = from d in db.ORDERs
                         where d.FinishWeek == finishWeek
                         select d;
            }
            else // all
            {
                orders = from d in db.ORDERs
                         select d;
            }

            foreach (var order in orders)
            {
                if (order.ORDER_PRODUCT != null)
                {
                    foreach (var order_product in order.ORDER_PRODUCT) // products in each order
                    {
                        if (kindId > 0 && String.IsNullOrEmpty(productId) == false)
                        {
                            if (order_product.Kind == kindId && order_product.IdProduct.Contains(productId))
                            {
                                if (order_product.BAGs != null)
                                {
                                    foreach (var bag in order_product.BAGs)
                                    {
                                        foreach (var pro in result)
                                        {
                                            if (pro.ProcessId == MCBagBO.GetCurrentProcessId(bag.Id))
                                            {
                                                // add quantity
                                                pro.Quantity += bag.Number;
                                                // add weight
                                                pro.Weight += bag.Current_Weight;
                                                // add bag to list
                                                pro.BagIds.Add(bag.Id);

                                                break;
                                            }
                                        } 
                                    }
                                }
                            }
                        }
                        else if (kindId > 0)
                        {
                            if (order_product.Kind == kindId)
                            {
                                if (order_product.BAGs != null)
                                {
                                    foreach (var bag in order_product.BAGs)
                                    {
                                        foreach (var pro in result)
                                        {
                                            if (pro.ProcessId == MCBagBO.GetCurrentProcessId(bag.Id))
                                            {
                                                // add quantity
                                                pro.Quantity += bag.Number;
                                                // add weight
                                                pro.Weight += bag.Current_Weight;
                                                // add bag to list
                                                pro.BagIds.Add(bag.Id);

                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else if (String.IsNullOrEmpty(productId) == false)
                        {
                            if (order_product.IdProduct.Contains(productId))
                            {
                                if (order_product.BAGs != null)
                                {
                                    foreach (var bag in order_product.BAGs)
                                    {
                                        foreach (var pro in result)
                                        {
                                            if (pro.ProcessId == MCBagBO.GetCurrentProcessId(bag.Id))
                                            {
                                                // add quantity
                                                pro.Quantity += bag.Number;
                                                // add weight
                                                pro.Weight += bag.Current_Weight;
                                                // add bag to list
                                                pro.BagIds.Add(bag.Id);

                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else // all
                        {
                            if (order_product.BAGs != null)
                            {
                                foreach (var bag in order_product.BAGs)
                                {
                                    foreach (var pro in result)
                                    {
                                        if (pro.ProcessId == MCBagBO.GetCurrentProcessId(bag.Id))
                                        {
                                            // add quantity
                                            pro.Quantity += bag.Number;
                                            // add weight
                                            pro.Weight += bag.Current_Weight;
                                            // add bag to list
                                            pro.BagIds.Add(bag.Id);

                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return result;
        }
    }
}